package com.nwpu.controller;

import com.nwpu.domain.Goods;
import com.nwpu.domain.SecKillGoods;
import com.nwpu.service.GoodsService;
import com.nwpu.util.RedisUtil;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author DPH
 * @date 2023/6/8
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedissonClient redissonClient;

    private boolean checkStart(String goodsId) {
        Goods goods = (Goods) redisUtil.get("GOODS_" + goodsId);
        if (goods == null) {
            return false;
        }
        if (goods.getBeginTime().after(new Date())) {
            return false;
        }
        return true;
    }

    private boolean checkMultipleBuying(String goodsId, String userId) {
        String key = "USER_HAS_SECKILL_" + userId + "_" + goodsId;
        if (redisUtil.get(key) != null) {
            return true;
        }
        redisUtil.set(key, "1");
        return false;
    }

    //判断库存是否充足
    private boolean checkStock(String goodsId) {
        String key = "GOODS_" + goodsId;
        SecKillGoods goods = (SecKillGoods) redisUtil.get(key);
        if (goods.getInventory() <= 0) {
            return false;
        }
        return true;
    }
}
